package com.twodudesdev.amerikick.learnkarate;

import com.twodudesdev.amerikick.learnkarate.DBAdapter;
import com.twodudesdev.amerikick.learnkarate.DBAdapter.DatabaseHelper;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
//import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class LocationActivity extends ListActivity {
	static final String ACTION = "com.twodudesdev.amerikick.learnkarate.LOCATION";
	
	static final String STRING_LOCATION = "com.twodudesdev.amerikick.learnkarate.string_location";
	
	public DBAdapter db_LearnKarate = (new DBAdapter(this, DatabaseHelper.DB_NAME, null, DatabaseHelper.DATABASE_VERSION));
	//private SQLiteDatabase db_LearnKarate;
	
	public Integer schoolId;
	public Integer intList;
	private String strLocation;
	public Cursor schoolCursor;
	public String[] from; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        setContentView(R.layout.location);

    	/** Logo Image */
        ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
        imageView1.setImageResource(R.drawable.locationpic);
        imageView1.setScaleType(ScaleType.FIT_XY);
    	
        setupDatabase();
    	
    	strLocation = getLocation();
    	
    	createList(strLocation);
    	
	}
    
	/**
     * Starts the AmerikickLocations for the specified location.
     *
     * @param context The application's environment.
     * @param user The location to display with a AmerikickLocation.
     */
    static void show(Context context, String location) {
        final Intent intent = new Intent(ACTION);
        intent.putExtra(STRING_LOCATION, location);
        context.startActivity(intent);
    }
	
	/**
     * Creates a Sting from the intent used to start this activity.
     *
     * @return The location to be displayed
     */
    private String getLocation() {
        final Intent intent = getIntent();
        final String action = intent.getAction();

        String location = null;

        if (ACTION.equals(action)) {
            final Bundle extras = intent.getExtras();
            if (extras != null) {
            	location = extras.getString(STRING_LOCATION);

                if (location == null) {
                    //location = "State";
                }
            }
        } /*else if (Intent.ACTION_VIEW.equals(action)) {
            final List<String> segments = intent.getData().getPathSegments();
            if (segments.size() > 1) {
                mUsername = segments.get(1);
            }
        }*/

        return location;
    }

    private void setupDatabase() {
		//this.deleteDatabase("com.amerikick.main.db");
    	
    	//addUsers();
    	//addSchools();
	}
    
    private void createList(String location) {
		// Query the Database for the States
    	db_LearnKarate.open();
    	
    	if (location == "AllStates") {
    		schoolCursor = db_LearnKarate.getStates();
    		from = new String[] { "st" };
    	} else {
    		schoolCursor = db_LearnKarate.getCities(location);
    		from = new String[] { "cty" };
    	}
    	
    	startManagingCursor(schoolCursor);
    	
		int[] to = new int[] { R.id.location_text };
		
		// Create a List adaptor bound to the cursor.
		// SimpleListAdaptor is designed for binding to a Cursor.
		ListAdapter stateAdapter = new SimpleCursorAdapter(
				this, 
				R.layout.list_items,
				schoolCursor,
				from, 
				to);
		
		setListAdapter (stateAdapter);
	}
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	    super.onListItemClick(l, v, position, id);

	    Cursor o = (Cursor) this.getListAdapter().getItem(position);
	    //Integer locationID = Integer.parseInt(o.getString(0));
	    String locationName = o.getString(1);

	    Toast.makeText(getApplicationContext(), locationName,
	    		Toast.LENGTH_SHORT).show();
	    
	    // Get instance of Vibrator from current Context
    	Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    	 
    	// Vibrate for 300 milliseconds
    	vib.vibrate(100);
	    
	    if (strLocation == "AllStates") {
	    	/*Intent location = new Intent(AmerikickLocations.this, AmerikickLocations.class);
			
			bdlLocation.putInt("location", 1);
			bdlLocation.putString("city", locationName);
			
			location.putExtras(bdlLocation);
			startActivity(location);)*/
	    	LocationActivity.show(this, locationName);
	    } else {
	    	/*Intent school = new Intent(AmerikickLocations.this, ShowSchool.class);
			
			bdlLocation.putInt("school", locationID);
			bdlLocation.putString("city", locationName);

			school.putExtras(bdlLocation);
			startActivity(school);*/
	    	AddressActivity.show(this, locationName);
	    }	
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
		// Handle item selection
	    switch (item.getItemId()) {
	    case R.id.contact_us:
	    	i = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amerikick.com/index.php?option=com_aicontactsafe&view=message&layout=message&pf=1&Itemid=55")));
	    	startActivity(i);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
    }
}
